/*The MIT License

Copyright (c) <1/27/2011> <erick.difiore>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/


package com.d4a5.mapping.esri
{
	import com.d4a5.mapping.core.IHighlightedObject;
	import com.d4a5.mapping.core.ISelectedObject;
	import com.esri.ags.Graphic;
	import com.esri.ags.geometry.MapPoint;
	import com.esri.ags.geometry.Polygon;
	import com.esri.ags.geometry.Polyline;
	import com.esri.ags.renderers.Renderer;
	import com.esri.ags.symbol.SimpleFillSymbol;
	import com.esri.ags.symbol.SimpleLineSymbol;
	import com.esri.ags.symbol.SimpleMarkerSymbol;
	import com.esri.ags.symbol.Symbol;
	
	public class wmeRenderer extends Renderer
	{
		public var innerRenderer:Renderer;
		public var defaultSymbol:Symbol;
		public var selectedSymbol:Symbol;
		public var highlightedSymbol:Symbol;
		public var bothSymbol:Symbol;
		
		protected var _attribute:String;
		protected var _lastAttribute:String;
		
		public function wmeRenderer()
		{
			super();
		}
		
		
		override public function getSymbol(graphic:Graphic):Symbol {
			var symbol:Symbol = this.defaultSymbol;

			if( this.innerRenderer ){
				symbol = this.innerRenderer.getSymbol( graphic );
			}
			
			if( symbol == null ){
				if( graphic.geometry is MapPoint ){
					symbol = new wmeMarkerSymbol();
				}
				else if( graphic is Polyline ){
					symbol = new wmeLineSymbol();
				}
				else if( graphic is Polygon ){
					symbol = new wmeFillSymbol();
				}
			}
			
			if( graphic is wmeGraphic ){
				var wmeG:wmeGraphic = graphic as wmeGraphic;
				
				if( wmeG.selected ){
					if( wmeG.highlighted && this.bothSymbol ){
						symbol = this.getNewSymbol( wmeG , symbol , this.bothSymbol );
					}
					else if( this.selectedSymbol ){
						symbol = this.getNewSymbol( wmeG , symbol , this.selectedSymbol );
					}
				}
				else if( wmeG.highlighted && this.highlightedSymbol ){
					symbol = this.getNewSymbol( wmeG , symbol , this.highlightedSymbol );
				}
			}
			return symbol;
		}
		
		
		protected function calculateColorWithAlpha( top:uint , bottom:uint , alpha:Number ) : uint {
			if( alpha == 1 ){
				return top;
			}
			else if( alpha == 0 ){
				return bottom;
			}
			
			var red:uint = ((((top >> 16) & 0xff) - ((bottom >> 16) & 0xff)) * alpha) + ((bottom >> 16) & 0xff);
			var green:uint = ((((top >> 8) & 0xff) - ((bottom >> 8) & 0xff)) * alpha) + ((bottom >> 8) & 0xff);
			var blue:uint = (((top & 0xff) - (bottom & 0xff)) * alpha) + (bottom & 0xff);
			
			return (((red & 0xff) << 16) | ((green & 0xff) << 8) | (blue & 0xff));
		}
		
		protected function getNewSymbol( graphic:wmeGraphic , symbol:Symbol , stateSymbol:Symbol ) : Symbol {
			var newSymbol:Symbol = symbol;
			
			
			var color:uint = this.calculateColorWithAlpha( (stateSymbol as Object).color,  (symbol as Object).color , (stateSymbol as Object).alpha );
			
		
			if( symbol is SimpleLineSymbol && stateSymbol is SimpleLineSymbol ){
				var sls:SimpleLineSymbol = symbol as SimpleLineSymbol;
				var slsState:SimpleLineSymbol = stateSymbol as SimpleLineSymbol;
				
				newSymbol = new wmeLineSymbol( slsState.style , color , sls.alpha , slsState.width );
			}
			else{
				var osls:SimpleLineSymbol = (symbol as Object).outline as SimpleLineSymbol;
				var oslsState:SimpleLineSymbol = (stateSymbol as Object).outline as SimpleLineSymbol;
				
				
				var outline:wmeLineSymbol;
				if( osls && oslsState ){
					var oColor:uint = this.calculateColorWithAlpha( oslsState.color,  osls.color , oslsState.alpha );
					outline = new wmeLineSymbol( oslsState.style , oColor , oslsState.alpha , oslsState.width );
				}
				else if( osls ){
					outline = new wmeLineSymbol( oslsState.style , osls.color , osls.alpha , osls.width );
				}
				else{
					outline = new wmeLineSymbol( oslsState.style , oslsState.color , oslsState.alpha , oslsState.width );
				}
				
				
				if( symbol is SimpleFillSymbol && stateSymbol is SimpleFillSymbol ){
					var sfs:SimpleFillSymbol = symbol as SimpleFillSymbol;
					var sfsState:SimpleFillSymbol = stateSymbol as SimpleFillSymbol;
					
					newSymbol = new wmeFillSymbol( sfsState.style , color , sfs.alpha  , outline );
				}
				else if( symbol is SimpleMarkerSymbol && stateSymbol is SimpleMarkerSymbol ){
					var sms:SimpleMarkerSymbol = symbol as SimpleMarkerSymbol;
					var smsState:SimpleMarkerSymbol = symbol as SimpleMarkerSymbol;
					
					newSymbol  = new wmeMarkerSymbol( smsState.style , sms.size ,  color , sms.alpha , smsState.xoffset , smsState.yoffset , smsState.angle , outline ) 
				}
			}
			
			return newSymbol;
		}
		
		
		
	}
}